package com.tc.mq;

import com.rabbitmq.client.Channel;
import com.tc.dao.OrderMapper;
import com.tc.pojo.Order;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class PayCustomer {


    public static final Integer ORDER_STATUS = 5;

    @Autowired
    private OrderMapper orderMapper;


    @Autowired
    private RedisTemplate redisTemplate;

//    @RabbitListener(queues = "dlq-pay-queue")
//    @RabbitHandler
//    public void handleLazyOrder(@Payload Map data, Channel channel, @Headers Map headers){
//        String orderNo = (String) data.get("orderNo");
//        Long userId = (Long) data.get("userId");
//        Long psId = (Long) data.get("psId");
//        Integer  productId = (Integer) data.get("productId");
//        System.out.println("处理延迟的订单："+orderNo);
//
//        Order order = orderMapper.selectByOrderOrderNo(orderNo);
//        if(order.getStatus().equals(0)){
//
//            orderMapper.updateStatusByOrderNo(orderNo,ORDER_STATUS);
//
//            //更新库存  +1
//            seckillMapper.updateCountById(psId,1);
//
//            //redis库存数据更新
//            redisTemplate.opsForList().leftPush("seckill:count:"+psId,productId);
//        }
//        Long id = (Long)headers.get(AmqpHeaders.DELIVERY_TAG);
//        try {
//            channel.basicAck(id,false);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
